import {EventEmitter} from "events";

export class SelectionModel extends EventEmitter {
    private textModel_: any;
    private selection_: { anchor: number; focus: number };

    constructor(textModel: any) {
        super();
        this.textModel_ = textModel
        this.selection_ = {anchor: 0, focus: 0}
    }

    getSelection(): { anchor: number; focus: number } {
        return {...this.selection_}
    }

    setSelection(selection: { anchor: any; focus: any; }) {
        if (selection && this.selection_ && selection.anchor === this.selection_.anchor && selection.focus === this.selection_.focus) {
            return
        }
        this.selection_ = selection ? {...selection} : {anchor: 0, focus: 0}
    }

    isCollapsed(): boolean {
        return this.selection_?.anchor === this.selection_?.focus
    }

    isBackward(): boolean {
        return this.selection_?.anchor > this.selection_?.focus
    }
}